from kivy.graphics.transformation import Matrix
from kivy.uix.scatter import Scatter
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.app import App

class RootWidget(Scatter):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)

        print kwargs

        self.do_rotation = False
        self.do_scale = False
        self.do_translation = False

        self.grid = GridLayout(size=map(lambda x: x/2, Window.size), cols=3, rows=4, spacing=(10, 10), padding=10)
        self.grid.add_widget(Button(text = '7'))
        self.grid.add_widget(Button(text = '8'))
        self.grid.add_widget(Button(text = '9'))
        self.grid.add_widget(Button(text = '4'))
        self.grid.add_widget(Button(text = '5'))
        self.grid.add_widget(Button(text = '6'))
        self.grid.add_widget(Button(text = '1'))
        self.grid.add_widget(Button(text = '2'))
        self.grid.add_widget(Button(text = '3'))
        self.grid.add_widget(Button(text = '0'))
        self.grid.add_widget(Button(text = '.'))

        self.scatter_btn = Button(text = '<-')
        self.scatter_btn.bind(on_release = self.upper_scatter)
        self.grid.add_widget(self.scatter_btn)

        self.add_widget(self.grid)

    def upper_scatter(self, button):
        mat = Matrix().translate(1, 0, 0)
        self.apply_transform(mat)

    def setup_mode(self, **kwargs):
        print('call setup_mode')


class MainWidget(GridLayout):
    def __init__(self, **kwargs):
        super(MainWidget, self).__init__(**kwargs)
        Window.set_vkeyboard_class(RootWidget)
        #self.kb = Window.request_keyboard(self._keyboard_closed, self, input_type='text')
        self.state_lab = Label(text = 'Keyboard On')
        self.add_widget(self.state_lab)
        self.add_widget(TextInput())

    def _keyboard_closed(self, *args):
        print args
        self.state_lab.text = 'Keyboard Closed'

if __name__ == '__main__':
    class TestApp(App):
        def build(self):
            return MainWidget(cols=2, rows=2, size=Window.size)

        def _keyboard_closed(self, keyboard):
            print('keyboard_closed')

    TestApp().run()
